package com.agent.common.utils;

import com.agent.common.constant.CommonConstants;

/**
 * 积分计算工具类
 */
public class CreditCalculator {

    /**
     * 计算生成视频所需积分
     * @param model 模型名称
     * @param duration 时长（秒）
     * @param resolution 分辨率（480P/720P/1080P）
     * @return 所需积分
     */
    public static int calculateVideoCredits(String model, int duration, String resolution) {
        if (CommonConstants.MODEL_SEEDANCE_LITE.equals(model)) {
            return calculateSeedanceLiteCredits(duration, resolution);
        } else if (CommonConstants.MODEL_SEEDANCE_PRO.equals(model)) {
            return calculateSeedanceProCredits(duration, resolution);
        } else if (CommonConstants.MODEL_VEO_BASIC.equals(model)) {
            return 10; // Veo Basic 固定10积分
        } else if (CommonConstants.MODEL_VEO_PREMIUM.equals(model)) {
            return 50; // Veo Premium 固定50积分
        }
        throw new IllegalArgumentException("Unsupported model: " + model);
    }

    /**
     * 视频生成-Seedance 1.0 Lite模型所需积分
     *
     * @param duration 时长（秒）
     * @param resolution 分辨率（480P/720P/1080P）
     * @return 积分数量
     */
    private static int calculateSeedanceLiteCredits(int duration, String resolution) {
        int baseCredits = 0;
        int resolutionFactor = 0;
        
        // 根据时长计算基础积分
        if (duration == 5) {
            baseCredits = 2;
        } else if (duration == 10) {
            baseCredits = 4;
        } else {
            throw new IllegalArgumentException("Invalid duration for Seedance Lite: " + duration);
        }
        
        // 根据分辨率计算因子
        switch (resolution) {
            case "480P":
                resolutionFactor = 0;
                break;
            case "720P":
                resolutionFactor = 6; // 720P比480P多6积分
                break;
            case "1080P":
                resolutionFactor = 16; // 1080P比480P多16积分
                break;
            default:
                throw new IllegalArgumentException("Invalid resolution: " + resolution);
        }
        
        return baseCredits + resolutionFactor;
    }

    /**
     * 视频生成-Seedance 1.0 Pro模型所需积分
     *
     * @param duration 时长（秒）
     * @param resolution 分辨率（480P/720P/1080P）
     * @return 积分数量
     */
    private static int calculateSeedanceProCredits(int duration, String resolution) {
        int baseCredits = 0;
        int resolutionFactor = 0;
        
        // 根据时长计算基础积分
        if (duration == 5) {
            baseCredits = 8;
        } else if (duration == 10) {
            baseCredits = 16;
        } else {
            throw new IllegalArgumentException("Invalid duration for Seedance Pro: " + duration);
        }
        
        // 根据分辨率计算因子
        switch (resolution) {
            case "480P":
                resolutionFactor = 0;
                break;
            case "720P":
                resolutionFactor = 10; // 720P比480P多10积分
                break;
            case "1080P":
                resolutionFactor = 24; // 1080P比480P多24积分
                break;
            default:
                throw new IllegalArgumentException("Invalid resolution: " + resolution);
        }
        
        return baseCredits + resolutionFactor;
    }

    /**
     * 计算生成图片所需积分
     * @param model 模型名称
     * @param quantity 生成数量
     * @return 所需积分
     */
    public static int calculateImageCredits(String model, int quantity) {
        if (CommonConstants.MODEL_GPT_IMAGE.equals(model)) {
            return calculateGptImageCredits(quantity);
        } else if (CommonConstants.MODEL_FLUX_PRO.equals(model)) {
            return calculateFluxProCredits(quantity);
        }
        throw new IllegalArgumentException("Unsupported model: " + model);
    }

    /**
     * 图片生成-GPT-Image-1模型所需积分
     * @param quantity 数量
     * @return 积分数量
     */
    private static int calculateGptImageCredits(int quantity) {
        switch (quantity) {
            case 1: return 4;
            case 2: return 8;
            case 3: return 12;
            case 4: return 16;
            default: throw new IllegalArgumentException("Invalid quantity for GPT-Image: " + quantity);
        }
    }

    /**
     * 图片生成-Flux Pro 1.1 Ultra模型所需积分
     * @param quantity 数量
     * @return 积分数量
     */
    private static int calculateFluxProCredits(int quantity) {
        switch (quantity) {
            case 1: return 12;
            case 2: return 24;
            case 3: return 38;
            case 4: return 52;
            default: throw new IllegalArgumentException("Invalid quantity for Flux Pro: " + quantity);
        }
    }
}